<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/27
 * Time: 9:51
 */

namespace app\api\controller;


use think\Config;
use think\Db;

class Timing
{
	/**
     * 下载地址
     */
	public function dowload() {//echo 1;die;
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            echo "<script> location.href='https://itunes.apple.com/cn/app/id1507081877'; </script>";
            // echo 'ios 下载地址';
        }else{
            echo "<script> location.href='http://ctruckapp.ctruck.com.cn/truckapp_3.apk'; </script>";
            // echo 'Android 下载地址';
        }

    }
    /**
     * 修改30分钟未付款的订单的状态&释放库存
     */
    public function closeNoPayOrder(){
        $begin_time = date('Y-m-d H:i:s', strtotime('-30 minutes')); // 30分钟之前
        $noPayOrder = Db::table('up_order')
            ->where('order_time', '< time', $begin_time)
            ->where('order_status', 1)
            ->field('order_goods_id, order_num, order_id')
            ->select();
        
        foreach($noPayOrder as $k => $v){
            $goods = Db::table('up_goods')->where('goods_id', $v['order_goods_id'])->find();
            Db::startTrans();
            try{
                // 更新状态--关闭
                Db::table('up_order')->where('order_id', $v['order_id'])->setField('order_status', 5);
                // 释放库存
                if($goods){
                    Db::table('up_goods')->where('goods_id', $v['order_goods_id'])->setInc('goods_stock', $v['order_num']);
                }
                Db::commit();
            }catch(\Exception $e){
                Db::rollback();
            }
        }
    }
    
    /**
     * 备份数据库
     * @return string
     */
    public function  copymysql()
    {
		set_time_limit(0);
		$today = date('Y-m-d');
		$dir = ROOT_PATH . "public/database/$today";
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        @$falg=file_get_contents(ROOT_PATH . "public/database/$today/database.sql");
        if($falg==true){
            unlink(ROOT_PATH . "public/database/$today/database.sql");
        }
        $config=Config::get('database');
        $link=mysqli_connect($config['hostname'],$config['username'],$config['password']);
        mysqli_select_db($link,$config['database']);
        $res=Db::query("show tables");
        $tabList=array();
        foreach($res as $k=>$v){
            foreach($v as $val){
                array_push($tabList,$val);}
        }
        $to_file_name = ROOT_PATH . "public/database/$today/database.sql";
        $info = "-- ----------------------------\r\n";
        $info .= "-- 日期：".date("Y-m-d H:i:s",time())."\r\n";
        $info .= "-- @qq.com\r\n";
        $info .= "-- ----------------------------\r\n\r\n";
        file_put_contents($to_file_name,$info,FILE_APPEND);

//将每个表的表结构导出到文件
        foreach($tabList as $val){
            $sql = "show create table ".$val;
            
            $row =Db::query($sql);
            
            $info = "-- ----------------------------\r\n";
            $info .= "-- Table structure for `".$val."`\r\n";
            $info .= "-- ----------------------------\r\n";
            $info .= "DROP TABLE IF EXISTS `".$val."`;\r\n";
            $sqlStr = $info.$row[0]['Create Table'].";\r\n\r\n";
            //追加到文件
            file_put_contents($to_file_name,$sqlStr,FILE_APPEND);
            
            //将每个表的数据导出到文件
            $sql = "select * from ".$val;
            $res =Db::query($sql);
            //如果表中没有数据，则继续下一张表
            if(empty($res)) continue;
            //
            $info = "-- ----------------------------\r\n";
            $info .= "-- Records for `".$val."`\r\n";
            $info .= "-- ----------------------------\r\n";
            file_put_contents($to_file_name,$info,FILE_APPEND);
            //读取数据
            
            foreach($res as $key=>$value){
                $row=array_values($value);
                $sqlStr = "INSERT INTO `".$val."` VALUES (";
                foreach($row as $zd){
                    $sqlStr .= "'".mysqli_escape_string($link,$zd)."', ";
                }
                //去掉最后一个逗号和空格
                $sqlStr = substr($sqlStr,0,strlen($sqlStr)-2);
                $sqlStr .= ");\r\n";
                file_put_contents($to_file_name,$sqlStr,FILE_APPEND);
            }
            //释放资源
            
            file_put_contents($to_file_name,"\r\n",FILE_APPEND);
            
        }
       
        return true;
        
    }
}